package com.ruoyi.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.domain.R;
import com.fasterxml.jackson.annotation.JsonView;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.JacPrintModel;
import com.ruoyi.system.service.IJacPrintModelService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 打印模板 提供者
 *
 * @author ruoyi
 * @date 2020-07-21
 */
@RestController
@RequestMapping("jacPrintModel")
public class JacPrintModelController extends BaseController
{

	@Autowired
	private IJacPrintModelService jacPrintModelService;

	/**
	 * 查询${tableComment}
	 */
	@GetMapping("get/{modelId}")
	public JacPrintModel get(@PathVariable("modelId") Long modelId)
	{
//		System.out.println(jacPrintModelService.selectJacPrintModelById(modelId));
		return jacPrintModelService.selectJacPrintModelById(modelId);

	}

//	/**
//	 * 通过菜单和基地获取打印模板
//	 */
//
//	@PostMapping("getModelByMeFa")
//	@JsonView(value = JacPrintModel.printModelIdView.class)
//	public R getModelByMeFa(@RequestBody JacPrintModel jacPrintModel)
//	{
//		return jacResult(jacPrintModelService.selectModelByMeFa(jacPrintModel));
//	}

	/**
	 * 通过菜单和基地获取打印模板
	 */

	@PostMapping("getModelByMeFa")
	@JsonView(value = JacPrintModel.printModelIdView.class)
	public R getModelByMeFa(@RequestBody JacPrintModel jacPrintModel)
	{
		List<JacPrintModel> jacPrintModels = jacPrintModelService.selectModelByMeFa(jacPrintModel);
		Map<String,List<JacPrintModel>> map = new HashMap<>();
		for (JacPrintModel printModel : jacPrintModels) {
			if(!map.containsKey(printModel.getModelName())){
				Map<String,List<JacPrintModel>> jacprintModelMap = new HashMap<>();
				List<JacPrintModel> list = new ArrayList<>();
				list.add(printModel);
				map.put(printModel.getModelName(),list);
			}else {
				List<JacPrintModel> jacPrintModels1 = map.get(printModel.getModelName());
				jacPrintModels1.add(printModel);
				map.put(printModel.getModelName(),jacPrintModels1);
			}
		}
		Set<Map.Entry<String, List<JacPrintModel>>> entries = map.entrySet();
		List<Map<String, Object>> collect = entries.stream().map(e -> {
			Map<String, Object> newMap = new HashMap<>();
			newMap.put("modelName", e.getKey());
			newMap.put("modelInfo", e.getValue());
			return newMap;
		}).collect(Collectors.toList());
//		return jacResult(jacPrintModelService.selectModelByMeFa(jacPrintModel));
		return resultObject(collect);
	}


	/**
	 * 查询打印模板列表
	 */
	@GetMapping("list")
	public R list(JacPrintModel jacPrintModel)
	{
		startPage();
        return result(jacPrintModelService.selectJacPrintModelList(jacPrintModel));
	}



	/**
	 * 新增保存打印模板
	 */
	@PostMapping("save")
	public R addSave(@RequestBody JacPrintModel jacPrintModel)
	{
		return toAjax(jacPrintModelService.insertJacPrintModel(jacPrintModel));
	}

	/**
	 * 修改模板状态
	 */
	@PostMapping("status")
	public R status(@RequestBody JacPrintModel jacPrintModel)
	{
		return toAjax(jacPrintModelService.changeStatus(jacPrintModel));
	}

	/**
	 * 修改保存打印模板
	 */
	@PostMapping("update")
	public R editSave(@RequestBody JacPrintModel jacPrintModel)
	{
		return toAjax(jacPrintModelService.updateJacPrintModel(jacPrintModel));
	}

	/**
	 * 删除${tableComment}
	 */
	@PostMapping("remove")
	public R remove(Long ids)
	{
		return toAjax(jacPrintModelService.deleteJacPrintModelById(ids));
	}

}
